use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;

/// Application configuration structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AppConfig {
    /// Configuration version for migration purposes
    pub version: String,
    
    /// Editor-specific settings
    pub editor: EditorConfig,
    
    /// UI and appearance settings
    pub appearance: AppearanceConfig,
    
    /// File handling settings
    pub files: FileConfig,
    
    /// Performance and system settings
    pub performance: PerformanceConfig,
    
    /// Extension and plugin settings
    pub extensions: ExtensionConfig,
    
    /// Advanced and experimental settings
    pub advanced: AdvancedConfig,
    
    /// User-defined custom settings
    pub custom: HashMap<String, serde_json::Value>,
}

impl Default for AppConfig {
    fn default() -> Self {
        Self {
            version: "1.0.0".to_string(),
            editor: EditorConfig::default(),
            appearance: AppearanceConfig::default(),
            files: FileConfig::default(),
            performance: PerformanceConfig::default(),
            extensions: ExtensionConfig::default(),
            advanced: AdvancedConfig::default(),
            custom: HashMap::new(),
        }
    }
}

/// Editor configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EditorConfig {
    /// Font family for editor
    pub font_family: String,
    
    /// Font size in pixels
    pub font_size: u32,
    
    /// Line height multiplier
    pub line_height: f32,
    
    /// Tab size in spaces
    pub tab_size: u32,
    
    /// Use spaces instead of tabs
    pub insert_spaces: bool,
    
    /// Auto-save interval in seconds (0 = disabled)
    pub auto_save_interval: u32,
    
    /// Enable word wrap
    pub word_wrap: bool,
    
    /// Show line numbers
    pub line_numbers: bool,
    
    /// Show minimap
    pub minimap: bool,
    
    /// Enable code folding
    pub code_folding: bool,
    
    /// Enable auto-completion
    pub auto_completion: bool,
    
    /// Enable syntax highlighting
    pub syntax_highlighting: bool,
    
    /// Enable bracket matching
    pub bracket_matching: bool,
    
    /// Cursor style
    pub cursor_style: CursorStyle,
    
    /// Cursor blink rate in milliseconds
    pub cursor_blink_rate: u32,
}

impl Default for EditorConfig {
    fn default() -> Self {
        Self {
            font_family: "JetBrains Mono".to_string(),
            font_size: 14,
            line_height: 1.4,
            tab_size: 4,
            insert_spaces: true,
            auto_save_interval: 30,
            word_wrap: false,
            line_numbers: true,
            minimap: true,
            code_folding: true,
            auto_completion: true,
            syntax_highlighting: true,
            bracket_matching: true,
            cursor_style: CursorStyle::Line,
            cursor_blink_rate: 500,
        }
    }
}

/// Cursor style options
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CursorStyle {
    Line,
    Block,
    Underline,
}

/// Appearance configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AppearanceConfig {
    /// Theme name
    pub theme: String,
    
    /// Color scheme
    pub color_scheme: ColorScheme,
    
    /// UI font family
    pub ui_font_family: String,
    
    /// UI font size
    pub ui_font_size: u32,
    
    /// Window transparency (0.0 - 1.0)
    pub window_transparency: f32,
    
    /// Enable animations
    pub animations: bool,
    
    /// Animation duration in milliseconds
    pub animation_duration: u32,
    
    /// Icon theme
    pub icon_theme: String,
    
    /// Sidebar position
    pub sidebar_position: SidebarPosition,
    
    /// Panel position
    pub panel_position: PanelPosition,
    
    /// Enable breadcrumbs
    pub breadcrumbs: bool,
    
    /// Enable status bar
    pub status_bar: bool,
}

impl Default for AppearanceConfig {
    fn default() -> Self {
        Self {
            theme: "auto".to_string(),
            color_scheme: ColorScheme::Auto,
            ui_font_family: "Inter".to_string(),
            ui_font_size: 13,
            window_transparency: 1.0,
            animations: true,
            animation_duration: 300,
            icon_theme: "default".to_string(),
            sidebar_position: SidebarPosition::Left,
            panel_position: PanelPosition::Bottom,
            breadcrumbs: true,
            status_bar: true,
        }
    }
}

/// Color scheme options
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ColorScheme {
    Light,
    Dark,
    Auto,
}

/// Sidebar position options
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SidebarPosition {
    Left,
    Right,
    Hidden,
}

/// Panel position options
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PanelPosition {
    Bottom,
    Right,
    Hidden,
}

/// File handling configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileConfig {
    /// Default encoding for files
    pub default_encoding: String,
    
    /// Auto-detect encoding
    pub auto_detect_encoding: bool,
    
    /// File associations
    pub associations: HashMap<String, String>,
    
    /// Excluded file patterns
    pub exclude_patterns: Vec<String>,
    
    /// Maximum file size for syntax highlighting (in MB)
    pub max_highlight_size: u32,
    
    /// Enable file watching
    pub file_watching: bool,
    
    /// Backup settings
    pub backup: BackupConfig,
    
    /// Recent files limit
    pub recent_files_limit: u32,
}

impl Default for FileConfig {
    fn default() -> Self {
        Self {
            default_encoding: "utf-8".to_string(),
            auto_detect_encoding: true,
            associations: HashMap::new(),
            exclude_patterns: vec![
                "node_modules".to_string(),
                ".git".to_string(),
                "target".to_string(),
                "*.tmp".to_string(),
            ],
            max_highlight_size: 10,
            file_watching: true,
            backup: BackupConfig::default(),
            recent_files_limit: 20,
        }
    }
}

/// Backup configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackupConfig {
    /// Enable automatic backups
    pub enabled: bool,
    
    /// Backup interval in minutes
    pub interval: u32,
    
    /// Maximum number of backups to keep
    pub max_backups: u32,
    
    /// Backup directory path
    pub backup_dir: Option<PathBuf>,
}

impl Default for BackupConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            interval: 10,
            max_backups: 10,
            backup_dir: None,
        }
    }
}

/// Performance configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceConfig {
    /// Maximum memory usage in MB
    pub max_memory_mb: u32,
    
    /// Enable performance monitoring
    pub monitoring: bool,
    
    /// Worker thread count (0 = auto)
    pub worker_threads: u32,
    
    /// Cache size in MB
    pub cache_size_mb: u32,
    
    /// Enable lazy loading
    pub lazy_loading: bool,
    
    /// Debounce delay in milliseconds
    pub debounce_delay: u32,
}

impl Default for PerformanceConfig {
    fn default() -> Self {
        Self {
            max_memory_mb: 1024,
            monitoring: false,
            worker_threads: 0,
            cache_size_mb: 256,
            lazy_loading: true,
            debounce_delay: 300,
        }
    }
}

/// Extension configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExtensionConfig {
    /// Enable extensions
    pub enabled: bool,
    
    /// Auto-update extensions
    pub auto_update: bool,
    
    /// Extension directories
    pub extension_dirs: Vec<PathBuf>,
    
    /// Disabled extensions
    pub disabled_extensions: Vec<String>,
}

impl Default for ExtensionConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            auto_update: true,
            extension_dirs: Vec::new(),
            disabled_extensions: Vec::new(),
        }
    }
}

/// Advanced configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdvancedConfig {
    /// Enable debug mode
    pub debug_mode: bool,
    
    /// Log level
    pub log_level: LogLevel,
    
    /// Enable experimental features
    pub experimental_features: bool,
    
    /// Telemetry settings
    pub telemetry: TelemetryConfig,
    
    /// Developer options
    pub developer: DeveloperConfig,
}

impl Default for AdvancedConfig {
    fn default() -> Self {
        Self {
            debug_mode: false,
            log_level: LogLevel::Info,
            experimental_features: false,
            telemetry: TelemetryConfig::default(),
            developer: DeveloperConfig::default(),
        }
    }
}

/// Log level options
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum LogLevel {
    Error,
    Warn,
    Info,
    Debug,
    Trace,
}

/// Telemetry configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TelemetryConfig {
    /// Enable telemetry
    pub enabled: bool,
    
    /// Include crash reports
    pub crash_reports: bool,
    
    /// Include usage statistics
    pub usage_stats: bool,
}

impl Default for TelemetryConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            crash_reports: false,
            usage_stats: false,
        }
    }
}

/// Developer configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeveloperConfig {
    /// Enable developer tools
    pub tools_enabled: bool,
    
    /// Show performance metrics
    pub show_metrics: bool,
    
    /// Enable hot reload
    pub hot_reload: bool,
}

impl Default for DeveloperConfig {
    fn default() -> Self {
        Self {
            tools_enabled: false,
            show_metrics: false,
            hot_reload: false,
        }
    }
}
